// 平台检测工具
class PlatformDetector {
  constructor() {
    this.platform = this.detectPlatform();
    this.environment = this.detectEnvironment();
  }

  // 检测平台类型
  detectPlatform() {
    // #ifdef H5
    return 'web';
    // #endif
    
    // #ifdef MP-WEIXIN
    return 'miniprogram';
    // #endif
    
    // #ifdef APP-PLUS
    return 'app';
    // #endif
    
    // #ifdef MP-ALIPAY
    return 'alipay';
    // #endif
    
    // #ifdef MP-BAIDU
    return 'baidu';
    // #endif
    
    // #ifdef MP-TOUTIAO
    return 'toutiao';
    // #endif
    
    // #ifdef MP-QQ
    return 'qq';
    // #endif
    
    return 'unknown';
  }

  // 检测运行环境
  detectEnvironment() {
    // #ifdef H5
    // 在H5环境下，进一步判断是否在微信浏览器中
    if (this.isWechatBrowser()) {
      return 'wechat-h5';
    }
    return 'browser';
    // #endif
    
    // #ifdef MP-WEIXIN
    return 'wechat-miniprogram';
    // #endif
    
    // #ifdef APP-PLUS
    return 'app';
    // #endif
    
    return 'unknown';
  }

  // 检测是否在微信浏览器中
  isWechatBrowser() {
    // #ifdef H5
    const ua = navigator.userAgent.toLowerCase();
    return ua.indexOf('micromessenger') !== -1;
    // #endif
    return false;
  }

  // 获取平台信息
  getPlatformInfo() {
    return {
      platform: this.platform,
      environment: this.environment,
      isWeb: this.platform === 'web',
      isMiniprogram: this.platform === 'miniprogram',
      isApp: this.platform === 'app',
      isWechat: this.environment.includes('wechat')
    };
  }

  // 检查是否支持特定功能
  supportsFeature(feature) {
    const supportMap = {
      'camera': this.platform === 'miniprogram' || this.platform === 'app',
      'location': this.platform === 'miniprogram' || this.platform === 'app',
      'storage': true, // 所有平台都支持
      'network': true, // 所有平台都支持
      'file': this.platform === 'web' || this.platform === 'app',
      'bluetooth': this.platform === 'miniprogram' || this.platform === 'app'
    };
    
    return supportMap[feature] || false;
  }

  // 获取平台特定的配置
  getPlatformConfig() {
    const configs = {
      'web': {
        defaultPage: '/pages/login/index',
        homePage: '/pages/admin/index',
        title: '新生报到系统 - Web端'
      },
      'miniprogram': {
        defaultPage: '/pages/dashboard/index',
        homePage: '/pages/dashboard/index',
        title: '新生报到系统'
      },
      'app': {
        defaultPage: '/pages/dashboard/index',
        homePage: '/pages/dashboard/index',
        title: '新生报到系统 - App端'
      }
    };
    
    return configs[this.platform] || configs['web'];
  }
}

export default new PlatformDetector(); 